package coumo.jdbc.driver;

import java.sql.*;
import java.util.Properties;
import java.util.ArrayList;
import java.util.StringTokenizer;
import coumo.jdbc.driver.*;
import coumo.jdbc.jci.*;

public class COUMODriver implements Driver
{
  // version
  public static final int major_version = 0;
  public static final int minor_version = 0;
  public static final int patch_version = 1;

  static final String build_serial_number = "0";

  // default connection informations
  public static final String default_hostname = "localhost";
  public static final int    default_port     = 3894;
  public static final String default_user     = "public";
  public static final String default_password = "";

  private final static String COUMO_JDBC_URL_HEADER   = "jdbc:coumo:";
  private final static String JDBC_DEFAULT_CONNECTION = "jdbc:default:connection";
  
  static
  {
    try
    {
      DriverManager.registerDriver(new COUMODriver());
    }
    catch (SQLException e)
    {
    }
  }

  /*
   * java.sql.Driver interface
   * format  jdbc:coumo:hostname:port:db_name:user:password
   */

  public Connection connect(String url, Properties info) throws SQLException
  {
    String hostname, db_name, user, passwd, dummy;
    int port;

    if (!acceptsURL(url))
    {
      throw new COUMOException(COUMOErrorCode.invalid_url, url);
    }

    if (url.toLowerCase().startsWith(JDBC_DEFAULT_CONNECTION))
    {
      return defaultConnection();
    }
    
    String[] url_arg = url.split(":");
    
    if ( url_arg.length < 7)
    {
      return null;
    }

    hostname = url_arg[2].length() == 0 ? default_hostname : url_arg[2];
    port     = url_arg[3].length() == 0 ? default_port     : Integer.valueOf(url_arg[3]);
    db_name  = url_arg[4].length() == 0 ? null             : url_arg[4];
    user     = url_arg[5].length() == 0 ? default_user     : url_arg[5];
    passwd   = url_arg[6].length() == 0 ? default_password : url_arg[6];

    // getting informations from the Properties object
    dummy = info.getProperty("user");
    if (dummy != null)
    {
      user = dummy;
    }

    dummy = info.getProperty("password");
    if (dummy != null)
    {
      passwd = dummy;
    }

    if (db_name == null)
    {
      throw new COUMOException(COUMOErrorCode.no_dbname);
    }

    if (user == null)
    {
      user = default_user;
    }

    if (passwd == null)
    {
      passwd = default_password;
    }
    
    return null;
  }

  public Connection defaultConnection() throws SQLException
  {
    return null;
  }

  public boolean acceptsURL(String url) throws SQLException
  {
    boolean r;
    
    r = url != null && ( 
           url.toLowerCase().startsWith(COUMO_JDBC_URL_HEADER)
        || url.toLowerCase().startsWith(JDBC_DEFAULT_CONNECTION) );

    return r;
  }

  public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
      throws SQLException
  {
    return new DriverPropertyInfo[0];
  }

  public int getMajorVersion()
  {
    return major_version;
  }

  public int getMinorVersion()
  {
    return minor_version;
  }

  public boolean jdbcCompliant()
  {
    return true;
  }
}
